/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.cx1.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.cx1.model.Lead;
import org.cx1.model.utils.ServiceResponse;
import org.cx1.model.utils.SystemProperties;
import org.cx1.service.api.ILeadService;
import org.cx1.service.impl.DLeadService;
import org.cx1.service.impl.MLeadService;
import org.cx1.web.controller.action.LeadAction;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
import org.springframework.web.servlet.view.RedirectView;

/**
 *
 * @author ECH
 */
public class LeadController extends BaseController {
    
    private ILeadService leadService = new DLeadService();
    
    public LeadController() {
        
    }
    
    protected ModelAndView handleRequestInternal( HttpServletRequest request,
                                                  HttpServletResponse response) throws Exception {
        ModelAndView mav = new ModelAndView();
        Map map = new HashMap();
        
        int action = StringUtils.isEmpty( request.getParameter("a") ) ? 0 : Integer.parseInt(request.getParameter("a"));
        
        switch (action) {
            case LeadAction.READ_ALL: {
                readAll(mav, map, request);
                break;
            }
            case LeadAction.CREATE: {
                add(mav, map, request);
                break;
            }
            case LeadAction.DELETE: {
                delete(mav, map, request);
                break;
            }
            default: {
                add(mav, map, request);
                break;
            }
        }        
        
        map.put("host", SystemProperties.SYSTEM_HOST);
        map.put("port", SystemProperties.SYSTEM_PORT);
        map.put("props", this.getSystemProperties());
        
        mav.addObject("model", map);
        return mav;
    }
    
    private void readAll(ModelAndView mav, Map map, HttpServletRequest request) {
        ServiceResponse<List<Lead>> response = leadService.readAll(0, 0);
        map.put("leads", response.getResponse());
        mav.setViewName("leads");
    }
    
    private void add(ModelAndView mav, Map map, HttpServletRequest request) {  
        
        if (!StringUtils.isEmpty(request.getParameter("s"))) {
            String name = request.getParameter("name");
            String email = request.getParameter("email");
            String phone = request.getParameter("phone");

            String term = request.getParameter("term");
            String type = request.getParameter("type");
            String city = request.getParameter("city");
            String neighborhood = request.getParameter("neighborhood");
            String structure = request.getParameter("structure");

            String bedsMin = request.getParameter("bedsMin");
            String bedsMax = request.getParameter("bedsMax");
            String priceMin = request.getParameter("priceMin");
            String priceMax = request.getParameter("priceMax");

            String description = request.getParameter("description");       
                
            Lead lead = new Lead();
            
            lead.setName(name);
            lead.setEmail(email);
            lead.setPhone(phone);
            
            lead.setTerm(term);
            lead.setType(type);
            
            lead.setCity(city);
            lead.setNeighborhood(neighborhood);
            lead.setStructure(structure);
            
            lead.setBedsMin(bedsMin);
            lead.setBedsMax(bedsMax);
            lead.setPriceMin(priceMin);
            lead.setPriceMax(priceMax);
            
            ServiceResponse<String> response1 = this.leadService.create(lead);
            mav.setView( new RedirectView("lead.htm"));
        }
        else {
            mav.setViewName("leadadd");            
        }
    }
    
    private void delete(ModelAndView mav, Map map, HttpServletRequest request) {  
        String id = request.getParameter("id");
        ServiceResponse<Boolean> response1 = leadService.delete(id);
        mav.setView( new RedirectView("lead.htm"));
    }
    
}